from PyQt4 import QtCore, QtGui
from ui_mainwindow import Ui_MainWindow
from ui_importgeodexdialog import Ui_GeodexDialog
import metadataeditor
import publishdialog
import importgeodexdialog
import sys
import os.path
import pickle

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.mdiArea = QtGui.QMdiArea()
        self.setCentralWidget(self.mdiArea)
        self.setWindowState(QtCore.Qt.WindowMaximized)

        self.connect(self.ui.action_Open, QtCore.SIGNAL("triggered()"),
                     self.open_file)

        self.connect(self.ui.action_Save, QtCore.SIGNAL("triggered()"),
                     self.save_file)

        self.connect(self.ui.actionPublish, QtCore.SIGNAL("triggered()"),
                     self.open_publish_dialog)

        self.connect(self.ui.action_New, QtCore.SIGNAL("triggered()"),
                     self.new_file)

        self.connect(self.ui.actionImport_GEODEX_Records, QtCore.SIGNAL("triggered()"),
                     self.import_geodex)

        self.connect(self.ui.actionAbout_Qt, QtCore.SIGNAL("triggered()"),
                     QtGui.QApplication.instance().aboutQt)
        
        self.connect(self.ui.actionAbout_MLMMS, QtCore.SIGNAL("triggered()"),
                     self.about_MLMMS)
        self.connect(self.mdiArea, QtCore.SIGNAL("subWindowActivated(QMdiSubWindow*)"),
                     self.window_activated)
        
    def window_activated(self, window):
        if window != None:
            self.ui.action_Save.setEnabled(True)
        else:
            self.ui.action_Save.setEnabled(False)
        
    def new_file(self):
        window = metadataeditor.Dialog()
        self.mdiArea.addSubWindow(window)
        window.show()

    def open_file(self):
        filename = QtGui.QFileDialog.getOpenFileName(None, "Open File", ".", "FGDC XML files (*.xml);;All Files (*.*)")
        if filename != '':
            try:
                window = metadataeditor.Dialog()
                self.mdiArea.addSubWindow(window)
                window.load_file(filename)
                window.show()
            except Exception, message:
                print message
                QtGui.QMessageBox.critical(self, "Error", "Invalid FGDC file.")

    def save_file(self):
        filename = QtGui.QFileDialog.getSaveFileName(None, "Save File", ".", "FGDC XML files (*.xml)")
        window = self.mdiArea.activeSubWindow().widget()
        window.write_file(filename)

    def open_publish_dialog(self):
        if self.mdiArea.activeSubWindow() != None:
            image_file = self.mdiArea.activeSubWindow().widget().ui.onlinkLineEdit.text()
            dir = QtCore.QDir(image_file)
            if dir.cdUp():
                image_dir = dir.path()
            else:
                image_dir = ''
        else:
            image_dir = ''
        window = publishdialog.Dialog(image_dir)
        window.exec_()

    def import_geodex(self):
        window = importgeodexdialog.ImportGeodexDialog()
        window.exec_()

    def about_MLMMS(self):
        QtGui.QMessageBox.about(self, 'About Metadata Editor', 'This Metadata Editor was written at the University of Connecticut.')

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
    

